package com.zhongjian.technologycal.mall.activity.main.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.activity.main.activity.CourseDetailsActivity;
import com.zhongjian.technologycal.mall.activity.main.bean.CatalogBean;
import com.zhongjian.technologycal.mall.adapter.CourseCatalogAdapter;
import com.zhongjian.technologycal.mall.base.BaseFragment;
import com.zhongjian.technologycal.mall.net.HttpRetrofitClient;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.ToastUitl;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import okhttp3.ResponseBody;
import rx.Subscriber;

/**
 * Created by wei on 2017/12/15.
 * 目录
 */

public class CourseDetailsCatalogFragment extends BaseFragment {
    public static final String CATEGORY_NAME = "com.nanchen.aiyagirl.module.category.CategoryFragment.CATEGORY_NAME";
    @BindView(R.id.catalog_recyclerview)
    RecyclerView catalog_recyclerview;
    private List<CatalogBean> listItems;
    private CourseCatalogAdapter courseCatalogAdapter;
    private String courseId;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    courseCatalogAdapter.notifyDataSetChanged();
                    break;
            }
        }
    };

    @Override
    protected void initView() {
        initViewRequest();
        listItems = new ArrayList<>();
        courseCatalogAdapter = new CourseCatalogAdapter(listItems);
        catalog_recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        catalog_recyclerview.setAdapter(courseCatalogAdapter);
        courseCatalogAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);

    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_course_details_catalog;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        courseId = ((CourseDetailsActivity) activity).getCourseId();
    }

    public static CourseDetailsCatalogFragment newInstance(String mCategoryName) {
        CourseDetailsCatalogFragment categoryFragment = new CourseDetailsCatalogFragment();
        Bundle bundle = new Bundle();
        bundle.putString(CATEGORY_NAME, mCategoryName);
        categoryFragment.setArguments(bundle);
        return categoryFragment;
    }

    @Override
    public void initPresenter() {

    }

    private void initViewRequest() {
        if (!NetWorkUtils.isNetConnected(getActivity())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        }
        HttpRetrofitClient.getInstance(getActivity())
                .getUrlRequestCoursedetailcatalog(Integer.valueOf(courseId),
                         new Subscriber<ResponseBody>() {
                            @Override
                            public void onStart() {
                                super.onStart();
                            }

                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onNext(ResponseBody responseBody) {
                                try {
                                    JSONObject jsonObject = new JSONObject(responseBody.string());
                                    JSONArray jsonRecommend = jsonObject.optJSONArray("data");
                                    Gson gson = new Gson();
                                    List<CatalogBean> forList = new ArrayList<>();
                                    for (int i = 0; i < jsonRecommend.length(); i++) {
                                        CatalogBean teacherBean = gson.fromJson(jsonRecommend.optString(i).toString(),
                                                CatalogBean.class);
                                        forList.add(teacherBean);

                                    }
                                    setData(forList);

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            }
                        });


    }
    private void setData(List<CatalogBean> foreshowList) {
        if (listItems.size() > 0) {
            listItems.clear();
        }
        for (CatalogBean vo : foreshowList) {
            listItems.add(vo);
        }
        handler.sendEmptyMessage(0);

    }


}
